<template>
	<div>
		<!-- 材料信息 -->
		<van-row>
			<van-col span="4">
				<van-image
				  style="margin-top: 10%;"
				  width="70"
				  height="70"
				  src="https://czsc.oss-cn-beijing.aliyuncs.com/mini/material_detail.png"
				/>
			</van-col>
			<van-col span="18" offset="0">
				<div style="margin-top: 5%;margin-left: 5%;font-size: 28rpx;">
					<div style="margin-bottom: 3%;">
						<span style="margin-right: 2%;">
							{{ material_name }}
						</span>
						<span>
							|
						</span>
						<span style="margin-left: 2%;">
							{{ material_model }}
						</span>
					</div>
					<div>
						<span style="color: #9d9c9c;margin-right: 3%;">实时库存</span>
						<span>
							{{ material_num }}{{ material_unit }}
						</span>
			 	</div>
				</div>
			</van-col>
		</van-row>
		<van-divider />
		<!-- 出入库 -->
		<div style="margin-bottom: 3%;">
			<div style="margin-left: 20%;">
				<van-button type="warning" color="#1989fa" style="margin-right: 40%;" @click="wareMaterial()">入库</van-button>
				<van-button type="warning" color="#07c160" @click="issueMaterial()">出库</van-button>
			</div>
			
		</div>
		<!-- 流水详细 -->
		<div v-for="(item, index) in material_details" :key="index" style="width: 90%;margin: auto">
			
			<div v-if="item.flag === '0'" style="margin-bottom: 3%;" @longpress="removeWare(item)">
				<div style="position: relative;;background-color: #eaf4fd;border-left: 5px solid #2d7ee9;border-radius: 5rpx;padding: 3%;">
					<div style="position: absolute;right: 3%;">
						<van-tag type="primary" size="large">入库</van-tag>
					</div>
					<p style="margin-bottom: 3%;">{{ item.time }}</p>
					<p style="margin-bottom: 3%;"><span style="margin-right: 2%;">供料单位:</span>{{ item.materialSource }}</p>
					<p style="margin-bottom: 3%;"><span style="margin-right: 2%;">合格证编号:</span>{{ item.sbpaCode }}</p>
					<p style="margin-bottom: 3%;"><span style="margin-right: 2%;">存放位置:</span>{{ item.place }}</p>
					<p style="margin-bottom: 3%;"><span style="margin-right: 2%;">收料数量:</span>{{ item.wareIssueNum }}{{ material_unit }}</p>
					<p><span style="margin-right: 2%;">车牌:</span>{{ item.car }}</p>
				</div>
			</div>
			<div v-if="item.flag === '1'" style="margin-bottom: 3%;" @longpress="removeIssue(item)">
				<div style="position: relative;background-color: #ebf9e8;;border-left: 5px solid #30c15b;border-radius: 5rpx;padding: 3%;">
					<div style="position: absolute;right: 3%;">
						<van-tag type="success" size="large">出库</van-tag>
					</div>
					<p style="margin-bottom: 3%;">{{ item.time }}</p>
					<p style="margin-bottom: 3%;"><span style="margin-right: 2%;">用料单位:</span>{{ item.materialSource }}</p>
					<p style="margin-bottom: 3%;"><span style="margin-right: 2%;">使用部位:</span>{{ item.place }}</p>
					<p style="margin-bottom: 3%;"><span style="margin-right: 2%;">发料数量:</span>{{ item.wareIssueNum }}{{ material_unit }}</p>
					<p><span style="margin-right: 2%;">车牌:</span>{{ item.car }}</p>
				</div>
			</div>
			
		</div>
	</div>
</template>

<script>
	import { BaseUrl } from '@/util/util.js';
	const PrefixUrl = BaseUrl();
	export default {
		data() {
			return {
				material_id: '',
				material_name: '',
				material_model: '',
				material_num: '',
				material_unit: '',
				material_details: [],
				material_decryptid: ''
			}
		},
		onShow(){
			uni.request({
				method: 'POST',
				url: PrefixUrl + '/materialWarehouse/getWareIssueByMaterialId',
				data: {
					material_id: this.material_id
				},
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'token': uni.getStorageSync('token')
				},
				success: (res) => {
					this.material_decryptid = res.data.obj.id;
					this.material_name = res.data.obj.materialName;
					this.material_model = res.data.obj.materialModel;
					this.material_num = res.data.obj.materialNum;
					this.material_unit = res.data.obj.materialUnit;
					this.material_details = res.data.obj2;
				}
			});
		},
		onLoad: function(option) {
			this.material_id = option.id;
			uni.request({
				method: 'POST',
				url: PrefixUrl + '/materialWarehouse/getWareIssueByMaterialId',
				data: {
					material_id: this.material_id
				},
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'token': uni.getStorageSync('token')
				},
				success: (res) => {
					this.material_name = res.data.obj.materialName;
					this.material_model = res.data.obj.materialModel;
					this.material_num = res.data.obj.materialNum;
					this.material_unit = res.data.obj.materialUnit;
					this.material_details = res.data.obj2;
				}
			});
		},
		created() {

		},
		methods: {
			wareMaterial(){
				uni.request({
					method: 'POST',
					url: PrefixUrl + '/materialWarehouse/decryptMaterialId',
					data: {
						material_id: this.material_id
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'token': uni.getStorageSync('token')
					},
					success: (res) => {
						uni.navigateTo({
							url: '/pages/material/warehouse/warehouse_detail?id=' + res.data.obj +
								'&name=' + this.material_name + '&model=' + this.material_model
						});
					}
				});
				
			},
			issueMaterial(){
				uni.request({
					method: 'POST',
					url: PrefixUrl + '/materialWarehouse/decryptMaterialId',
					data: {
						material_id: this.material_id
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'token': uni.getStorageSync('token')
					},
					success: (res) => {
						uni.navigateTo({
							url: '/pages/material/issue/issue_detail?id=' + res.data.obj +
								'&name=' + this.material_name + '&model=' + this.material_model
								+ '&num=' + this.material_num
						});
					}
				});
			},
			removeWare(item){
				const that = this;
				if (uni.getStorageSync('userPermission').indexOf('4') >= 0){
					uni.showModal({
					    title: '提示',
					    content: '是否删除入库',
					    success: function (res) {
					        if (res.confirm) {
					            uni.request({
					            	method: 'POST',
					            	url: PrefixUrl + '/materialWarehouse/removeWareById',
					            	data: {
					            		id: item.id,
										material_num: item.wareIssueNum,
										material_id: that.material_decryptid
					            	},
					            	header: {
					            		'content-type': 'application/x-www-form-urlencoded',
					            		'token': uni.getStorageSync('token')
					            	},
					            	success: (res) => {
					            		if (res.data.obj) {
					            			uni.showToast({
					            				title: '删除成功!',
					            				mask: true,
					            				icon: 'none'
					            			});
											uni.request({
												method: 'POST',
												url: PrefixUrl + '/materialWarehouse/getWareIssueByMaterialId',
												data: {
													material_id: that.material_id
												},
												header: {
													'content-type': 'application/x-www-form-urlencoded',
													'token': uni.getStorageSync('token')
												},
												success: (res) => {
													that.material_name = res.data.obj.materialName;
													that.material_model = res.data.obj.materialModel;
													that.material_num = res.data.obj.materialNum;
													that.material_unit = res.data.obj.materialUnit;
													that.material_details = res.data.obj2;
												}
											});
					            		}
					            	}
					            });
					        } else if (res.cancel) {}
					    }
					});
				}else {
					uni.showToast({
						title: '暂无权限，请联系科技信息部!',
						mask: true,
						icon: 'none'
					})
				}
			},
			removeIssue(item){
				const that = this;
				if (uni.getStorageSync('userPermission').indexOf('4') >= 0){
					uni.showModal({
					    title: '提示',
					    content: '是否删除出库',
					    success: function (res) {
					        if (res.confirm) {
					            uni.request({
					            	method: 'POST',
					            	url: PrefixUrl + '/materialIssue/removeIssueById',
					            	data: {
					            		id: item.id,
										material_num: item.wareIssueNum,
										material_id: that.material_decryptid
					            	},
					            	header: {
					            		'content-type': 'application/x-www-form-urlencoded',
					            		'token': uni.getStorageSync('token')
					            	},
					            	success: (res) => {
					            		if (res.data.obj) {
					            			uni.showToast({
					            				title: '删除成功!',
					            				mask: true,
					            				icon: 'none'
					            			});
											uni.request({
												method: 'POST',
												url: PrefixUrl + '/materialWarehouse/getWareIssueByMaterialId',
												data: {
													material_id: that.material_id
												},
												header: {
													'content-type': 'application/x-www-form-urlencoded',
													'token': uni.getStorageSync('token')
												},
												success: (res) => {
													that.material_name = res.data.obj.materialName;
													that.material_model = res.data.obj.materialModel;
													that.material_num = res.data.obj.materialNum;
													that.material_unit = res.data.obj.materialUnit;
													that.material_details = res.data.obj2;
												}
											});
					            		}
					            	}
					            });
					        } else if (res.cancel) {}
					    }
					});
				}else {
					uni.showToast({
						title: '暂无权限，请联系科技信息部!',
						mask: true,
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style>
</style>
